<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    style="width: 900px"
    draggable
    align-center
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="标题" prop="title">
        <!-- 注释掉el-input，并用span标签代替 -->
        <!-- <el-input
          v-model="formData.title"
          placeholder="请输入标题"
          :readonly="true"
        /> -->
        <span>{{ formData.title }}</span>
      </el-form-item>
      <el-form-item label="类型" prop="noticeTypeLable">
        <!-- 注释掉el-select，并用span标签代替 -->
        <!-- <el-select
          v-model="formData.noticeType"
          placeholder="请选择类型"
          :disabled="true"
          :readonly="true"
        >
          <el-option
            v-for="item in list"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select> -->
        <span>{{ formData.noticeTypeLable }}</span>
      </el-form-item>
      <el-form-item label="内容" prop="noticeContent">
        <!-- <sysnotice-wangeditor :modelValue="formData.noticeContent" /> -->
        <WangEditor
          :toolbarShow="false"
          :readonly="true"
          :modelValue="formData.noticeContent"
          :width="'750px'"
          :height="'300px'"
        />

        <!-- 注释掉el-input，并用span标签代替 -->
        <!-- <el-input
          v-model="formData.noticeContent"
          placeholder="请输入内容"
          :readonly="true"
        /> -->
        <!-- <span>{{ formData.noticeContent }}</span> -->
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <!-- 注释掉el-input，并用span标签代替 -->
        <!-- <el-input
          v-model="formData.remarks"
          placeholder="请输入备注"
          :readonly="true"
        /> -->
        <span>{{ formData.remarks }}</span>
      </el-form-item>
      <!-- <el-form-item label="是否启用" prop="enabled">
        注释掉el-switch，并用span标签代替 
       <el-switch
          v-model="formData.enabledStr"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :disabled="true"
        />
        <span>{{ formData.enabledStr ? "已启用" : "已禁用" }}</span>
      </el-form-item> -->
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";

defineOptions({
  name: "SYSNOTICEDETAIL",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  title: [{ required: true, message: "请输入标题", trigger: "blur" }],
  noticeType: [{ required: true, message: "请输入类型", trigger: "blur" }],
  noticeContent: [{ required: true, message: "请输入内容", trigger: "blur" }],
});

const formRef = ref(ElForm);

const list = ref([]);

let formData = reactive({
  id: "",
  version: "",
  title: "",
  noticeTypeLable: "",
  noticeContent: "",
  remarks: "",
  deleted: "",
  enabled: "",
  createTime: "",
  createdUserId: "",
  lastUpdatedUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserName: "",
  enabledStr: true,
});

watch(
  () => props.data,
  (newVal: any) => {
    if (newVal?.id) {
      const {
        id,
        version,
        title,
        noticeTypeLable,
        noticeContent,
        remarks,
        deleted,
        enabled,
        createTime,
        createdUserId,
        lastUpdatedUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        title,
        noticeTypeLable,
        noticeContent,
        remarks,
        deleted,
        enabled,
        createTime,
        createdUserId,
        lastUpdatedUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserName,
      });
      if (formData.enabled == "0") {
        formData.enabledStr = false;
      } else {
        formData.enabledStr = true;
      }
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.title = "";
  formData.noticeTypeLable = "";
  formData.noticeContent = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.enabled = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.lastUpdatedUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      if (formData.enabledStr) formData.enabled = "1";
      else formData.enabled = "0";
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/system/sysnotice/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table");
    }
  });
}

/** 查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/system/sysnotice/getDetails",
    method: "post",
    data: formData,
  });
}

onMounted(async () => {
  gtList();
  if (formData.id) {
    getDatails();
  }
});

// const list = await getDictData("NoticeTypee");

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

async function gtList() {
  const type = await request({
    url: `/system/Dictionary/getSelectedItems/NoticeType`,
    method: "post",
  });

  list.value = type.data;
}
/** 字典接口及数据处理 */
// function getDictData(id: string) {
//   return new Promise(async (resolve) => {
//     const { data } = await request({
//       url: `/system/Dictionary/getSelectedItems/${id}`,
//       method: "post",
//     });
//     resolve(data);
//   });
// }
</script>
